package DataStructure;

/**
 *
 *链表模拟栈
 * @time: 2021/3/28/028 22:08
 * @author: Zhurunhe
 */
public class ListStackTest {
    public static void main(String[] args) {
        ListStack listStack=new ListStack();
        NStack n1=new NStack(1,100);
        NStack n2=new NStack(2,200);
        NStack n3=new NStack(3,300);
        NStack n4=new NStack(4,400);
        NStack n5=new NStack(5,500);

        listStack.push(n1);
        listStack.push(n2);
        listStack.push(n3);
        listStack.push(n4);
        listStack.push(n5);
        listStack.show();

        System.out.println("---------");
        listStack.pop();
        listStack.pop();

        System.out.println("---------");
        listStack.show();

    }
}
class ListStack{
    private NStack first=new NStack(0,0);

    public void push(NStack n){
        NStack temp=first;
        while (true){
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        temp.next=n;
    }

    public void pop(){
        if (first.next==null){
            return;
        }
        NStack temp=first.next;
        while (true){
            if (temp.next.next==null){
                break;
            }

            temp=temp.next;
        }
        System.out.println("出栈"+temp.next.toString());
        temp.next=null;
    }


    public void show(){
        if(first==null||first.next==null){
            return;
        }
        NStack Nfirst=new NStack(0,0);
        NStack temp=first.next;
        NStack Next=null;
        while (temp!=null){
            Next=temp.next;
            temp.next=Nfirst.next;
            Nfirst.next=temp;
            temp=Next;
        }

        NStack temp1=Nfirst.next;
        while (temp1!=null){

            System.out.println(temp1.toString());
            temp1=temp1.next;
        }
        first.next=Nfirst.next;
    }
}
class NStack{
    int id;
    int values;
    NStack next;
    public NStack(int id,int values){
        this.id=id;
        this.values=values;
    }

    @Override
    public String toString() {
        return "NStack{" +"id=" + id +", values=" + values  +'}';
    }
}


